function [vtl_updates] = create_vtl_updates(test,lat,lon)

% input:
% test: number of the test
%   lat - N-by-2 matrix where V is the number of VTLs we want to create
%       updates for, the first column is the starting latitude, the second
%       column is the ending latitude
%   lon - N-by-2 matrix (same as lat except for the longitudes of the VTLs)
%   
%   VTLs around the San Pablo/University Intersection have already been
%   identified and are located in the VTL.mat file.  There are 4 in that
%   file, the first is the upstream VTL, second is for right turns, third
%   for straight through and fourth for left turns.  If you load VTL.mat
%   you will have the lat, lon variables needed to run this function

% output:
%   vtl_updates - N-by-1 cell array where each cell entry contains a L-by-4
%       matrix, where L is the number of updates for the corresponding VTL.
%       The 4 columns are vehicle number, direction of crossing, time
%       crossing (seconds), speed when crossing (mph)

allvehs = get_allvehs(test);

if size(lat,2) ~= 2 || size(lon,2) ~= 2
    error('lat and lon must be *-by-2 matrices');
end

N = size(lat,1);
if size(lon,1) ~= N
    error('lat and lon must be the same length');
end

vtl_updates = cell(N,1);

for i = 1:N
    vtl_updates{i} = zeros(1,4);
    index = 1;
    lastupdatetime = -1;
    for v = 2:size(allvehs,1)
        if allvehs(v,1) == allvehs(v-1,1)
            [has_crossed,direction,time_fraction,dist] = vehicle_crossed_vtl(lat(i,:),lon(i,:),[allvehs(v-1,3) allvehs(v,3)],[allvehs(v-1,4) allvehs(v,4)]);
            if has_crossed && allvehs(v,2)-lastupdatetime > 30
                vtl_updates{i}(index,1) = allvehs(v,1);
                vtl_updates{i}(index,2) = direction;
                vtl_updates{i}(index,3) = allvehs(v-1,2) + time_fraction*(allvehs(v,2)-allvehs(v-1,2));
                if test==3
                    conv = 3600/5280;
                else
                    conv = 3600/5280;
                end
                vtl_updates{i}(index,4) = conv*(dist/(allvehs(v,2)-allvehs(v-1,2)));
                index = index+1;
                lastupdatetime = allvehs(v,2);
            end
        else
            lastupdatetime = -1;
        end
    end
end